{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 算法题1\n",
    "\n",
    "给定一种规律 pattern 和一个字符串 str ，判断 str 是否遵循相同的规律。\n",
    "\n",
    "这里的 遵循 指完全匹配，例如， pattern 里的每个字母和字符串 str 中的每个非空单词之间存在着双向连接的对应规律。\n",
    "\n",
    "示例1:\n",
    "输入: pattern = \"abba\", str = \"dog cat cat dog\"\n",
    "输出: true\n",
    "\n",
    "示例 2:\n",
    "输入:pattern = \"abab\", str = \"dog cat dog cat\"\n",
    "输出: true\n",
    "\n",
    "示例 3:\n",
    "输入: pattern = \"aaaa\", str = \"dog cat cat dog\"\n",
    "输出: false\n",
    "\n",
    "示例 4:\n",
    "输入: pattern = \"abba\", str = \"dog dog dog dog\"\n",
    "输出: false\n",
    "\n",
    "写一个函数 接收两个参数 pattern 和 str ,如果pattern和str满足相同规律输出true,不同规律则输出false"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T12:30:32.635309Z",
     "start_time": "2022-01-20T12:30:32.627309Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['test', 'cat', 'cat', 'dog']"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "['dog', 'cat', 'cat', 'dog']"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "string1 = \"dog cat cat dog\"\n",
    "string1_list = string1.split()\n",
    "tmp_list = string1_list[::]\n",
    "string1_list[0]= 'test'\n",
    "string1_list\n",
    "tmp_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T12:41:31.495909Z",
     "start_time": "2022-01-20T12:41:31.482909Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def judg_same(pattern:str,string:str)->bool:\n",
    "    tmp_list = string.split()\n",
    "    string_list = list(set(string.split()))\n",
    "    # string_list.sort(key = string.index)\n",
    "    string_list.sort(key = lambda x:tmp_list.index(x))\n",
    "    map_dict = {k:v for k,v in zip(string_list,list('ab'))}\n",
    "    new_string =  ''.join([map_dict[x] for x in string.split()])\n",
    "\n",
    "    if new_string == pattern:\n",
    "        return True\n",
    "    else:\n",
    "        return False\n",
    "\n",
    "judg_same('abab','fish pig fish pig')\n",
    "judg_same('aaaa','dog cat cat dog')\n",
    "judg_same('abba','cat dog dog cat')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T12:47:07.591909Z",
     "start_time": "2022-01-20T12:47:07.581909Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 解法二:\n",
    "pattern = \"aaaa\" \n",
    "string = \"dog cat dog cat\"\n",
    "\n",
    "li1 =  list(pattern)\n",
    "li2 =  string.split()\n",
    "source = li1[0]\n",
    "res = []\n",
    "for i in range(1,len(li1)):\n",
    "    res.append(source == li1[i])\n",
    "\n",
    "source2 = li2[0]    \n",
    "res2 = []\n",
    "for i in range(1,len(li2)):\n",
    "    res2.append(source2 == li2[i])\n",
    "res == res2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T23:47:11.194802Z",
     "start_time": "2022-01-20T23:47:11.177802Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 解法三\n",
    "pattern = \"abba\"\n",
    "string = \"dog cat cat dog\"\n",
    "\n",
    "def same_text(pattern:str,string:str)->bool:\n",
    "    pattern_li = list(pattern)\n",
    "    string_li = string.split()\n",
    "    pattern_dict = {}\n",
    "    for index,item in enumerate(pattern_li):\n",
    "        if item not in pattern_dict.keys():\n",
    "            pattern_dict[item] = index\n",
    "    new_pattern = ''.join([str(pattern_dict[x]) for x in pattern_li])\n",
    "\n",
    "    string_dict = {}\n",
    "    for index,item in enumerate(string_li):\n",
    "        if item not in string_dict.keys():\n",
    "            string_dict[item] = index\n",
    "    new_string = ''.join([str(string_dict[x]) for x in string_li])\n",
    "    if new_pattern == new_string:\n",
    "        return True\n",
    "    else:\n",
    "        return False\n",
    "    \n",
    "same_text('abab','fish pig fish pig')\n",
    "same_text('abba','cat dog dog cat')\n",
    "same_text('aaaa','dog cat cat dog')\n",
    "same_text('abbc','dog cat cat fish')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 算法题2\n",
    "\n",
    "给定一个非空字符串 s，最多可以删除一个字符。判断是否能成为回文字符串。\n",
    "\n",
    "示例 1:\n",
    "输入: s = \"aba\"\n",
    "输出: true\n",
    "\n",
    "示例 2:\n",
    "输入: s = \"abca\"\n",
    "输出: true\n",
    "解释: 删除b或c字符,就是回文\n",
    "\n",
    "示例 3:\n",
    "输入: s = \"abc\"\n",
    "输出: false"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T13:04:14.643110Z",
     "start_time": "2022-01-20T13:04:14.635110Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'12'"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "'21'"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "string = \"412321\" \n",
    "s1 = string[:int(len(s)/2)]\n",
    "s2 = string[int(len(s)/2)+2:]\n",
    "s1 \n",
    "s2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T13:16:09.367311Z",
     "start_time": "2022-01-20T13:16:09.360311Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def same(string:str)->bool:\n",
    "    li_tmp = list(s)\n",
    "    res = []\n",
    "    if len(s)%2 == 0:\n",
    "        for i in range(int(len(li_tmp)/2)-1):\n",
    "            res.append(li_tmp[i] == li_tmp[-(i+1)])\n",
    "    else:\n",
    "        for i in range(int((len(li_tmp)+1)/2)-1):\n",
    "            res.append(li_tmp[i] == li_tmp[-(i+1)])\n",
    "    result = True\n",
    "    for item in res:\n",
    "        result &= item\n",
    "    return result\n",
    "\n",
    "# same(\"aba\")\n",
    "# same(\"abca\")\n",
    "# same(\"aabcaa\")\n",
    "# same(\"aabaa\")\n",
    "# same(\"abc\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T13:24:47.258511Z",
     "start_time": "2022-01-20T13:24:47.236511Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def same(string:str)->bool:\n",
    "    res = []\n",
    "    li = list(string)\n",
    "    for item in li:\n",
    "        tmp = list(string)\n",
    "        tmp.remove(item)\n",
    "        new_string = ''.join(tmp)\n",
    "        res.append(new_string == new_string[::-1])\n",
    "    result = False\n",
    "    for item in res:\n",
    "        result |= item\n",
    "    return result\n",
    "\n",
    "same('412321')    \n",
    "same('123214')   \n",
    "same('123421')     \n",
    "same('12321')  \n",
    "same('abc')  \n",
    "same('abcd')  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 算法题3\n",
    "\n",
    "集合 s 包含从 1 到 n 的整数。不幸的是，因为数据错误，导致集合里面某一个数字复制了成了集合里面的另外一个数字的值，导致集合 丢失了一个数字 并且 有一个数字重复 。\n",
    "给定一个数组 nums 代表了集合 S 发生错误后的结果。\n",
    "请你找出重复出现的整数，再找到丢失的整数，将它们以元组的形式返回。元组第一个数是重复出现的数，第二个数是丢失的数\n",
    "\n",
    "示例 1：\n",
    "输入：nums = [1,2,2,4]\n",
    "输出：(2,3)\n",
    "\n",
    "示例 2：\n",
    "输入：nums = [1,1]\n",
    "输出：(1,2)\n",
    "\n",
    "示例 2：\n",
    "输入：nums = [1,2,3,3]\n",
    "输出：(3,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-20T23:37:40.463601Z",
     "start_time": "2022-01-20T23:37:40.445601Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 3)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "(1, 2)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "(3, 4)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def repeat_num(nums:list)->tuple:\n",
    "    right_set = set(list(range(1,len(nums)+1)))\n",
    "    wrong_set = set(nums)\n",
    "    miss = right_set - wrong_set\n",
    "    miss = list(miss)[0]\n",
    "    counts = [nums.count(x) for x in nums]\n",
    "    for item in counts:\n",
    "        if item>1:\n",
    "            repeat = nums[counts.index(item)]\n",
    "    return (repeat,miss)\n",
    "\n",
    "repeat_num([1,2,2,4])\n",
    "repeat_num([1,1])\n",
    "repeat_num([1,2,3,3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
